/**
  * Author: Mei
  * Date: 2021-01-10 14:11
  * Description: 
  *
  */
#include "MessageBus.cpp"

MessageBus g_bus;
const string Topic = "Drive";
const string CallBackTopic = "DriveOK";

struct Subject {
    Subject() {
        g_bus.Attach([this] {
            DriveOK();
        }, CallBackTopic);
    }

    void SendReq(const string &topic) {
        g_bus.SendReq<void, int>(50, topic);
    }

    void DriveOK() {
        cout << "drive ok" << endl;
    }
};

struct Car {
    Car() {
        g_bus.Attach([this](int speed) {
            Drive(speed);
        }, Topic);
    }

    void Drive(int speed) {
        cout << "Car drive " << speed << endl;
        g_bus.SendReq<void>(CallBackTopic);
    }
};

struct Bus {
    Bus() {
        g_bus.Attach([this](int speed) {
            Drive(speed);
        });
    }

    void Drive(int speed) {
        cout << "Bus drive " << speed << endl;
        g_bus.SendReq<void>(CallBackTopic);
    }
};

struct Truck {
    Truck() {
        g_bus.Attach([this](int speed) {
            Drive(speed);
        });
    }

    void Drive(int speed) {
        cout << "Truck drive " << speed << endl;
        g_bus.SendReq<void>(CallBackTopic);
    }
};

//测试
int main() {
    Subject subject;
    Car car;
    Bus bus;
    Truck truck;
    subject.SendReq(Topic);
    subject.SendReq("");

    return 0;
}